const app = getApp()
import Toast from '@vant/weapp/toast/toast';
import {
  myPost,
  promiseGet
} from '../../request/index'
import {
  baseRecUrl,
  getShareId,
  updateUserName
} from '../../request/api'
Page({
  data: {
    isOverShare: true,
    baseRecUrl: baseRecUrl,
    codeViewFlag: false,
    infoList: [],
    imgUrl: '',
    homeImgs: [],
    type: '',
    CustomBar: app.globalData.statusBarHeight + 44
  },
  closeSignBg() {
    this.setData({
      codeViewFlag: false
    })
  },
  clickShare() {
    const uid = wx.getStorageSync('shareIdStr')
    if (uid) {
      myGet(clickShare + '/' + uid, {}, (res) => {
        if (res.status === 200) {
          wx.removeStorageSync('shareIdStr')
        }
      })
    } else {

    }
  },
  onShow() {},
  onPageScroll(e) {
    const navigationBar = this.selectComponent("#navigationBar")
    navigationBar.onScroll(e.scrollTop)
  },
  onShareAppMessage: async function (res) {
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo.name === "微信用户") {
      wx.showModal({
        title: '默认姓名(微信用户)无法分享，请修改',
        editable: true,
        placeholderText: '请输入姓名',
        complete: (res) => {
          if (res.cancel) {
            Toast("默认姓名(微信用户)将无法使用分享功能")
          } else if (res.confirm) {
            const name = res.content
            myPost(updateUserName + '?name=' + name, {}, (res) => {
              if (res.status === 200) {
                userInfo.name = name
                wx.setStorageSync('userInfo', userInfo)
                Toast.success("修改成功")
              }
            })
          }
        }
      })
      throw new Error("请先修改姓名")
    }
    let rdata = {}
    if (res.from !== 'menu') {
      const {
        data: shareId
      } = await promiseGet(getShareId)
      //如果用户是点击按钮进行分享的
      const imageUrl = baseRecUrl + '/imgs/share-img.png'
      if (res.from == 'button') {
        const path = "/pages/receive/index?shareId=" + shareId
        console.info('分享的链接:' + path);
        rdata = {
          title: userInfo.name + '的推荐链接', //分享出去的标题
          imageUrl: imageUrl, //分享时显示的图片
          path: path //别人点击链接进来的页面及传递的参数
        }
      } else {
        const path = "/pages/receive/index?shareId=" + shareId
        console.info('分享的链接:' + path);
        rdata = {
          title: userInfo.name + '的推荐链接', //分享出去的标题
          imageUrl: imageUrl, //分享时显示的图片
          path: path //别人点击链接进来的页面及传递的参数
        }
      }
    }
    return rdata
  },
})